﻿/*********************************************************************************/
/**    Sims3Pack Readme Generator                                               **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/




using System;
using System.Data;
using System.Windows.Forms;
using Sims3PackReadmeGenerator.Sims3PackReader;
using Sims3PackReadmeGenerator.language;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;

namespace Sims3PackReadmeGenerator.Gui
{
    /// <summary>
    /// form for managing the information about known packages
    /// </summary>
    public partial class ManageKnownPackagesInfoGui : Form
    {
        #region event

        public delegate void ManageKnownPackagesEventHandler(object sender, EventArgs e);
        public event ManageKnownPackagesEventHandler OnPackagesInfoUpdateHandler;

        protected void OnPackagesInfoUpdate()
        {
            if (OnPackagesInfoUpdateHandler != null)
                OnPackagesInfoUpdateHandler(this, new EventArgs());
        }

        #endregion

        #region fields

        private bool _isFormLoading = false;
        private bool _isDataUpdated = false; 
        private DataSet             _dataSetPackages;             
        private List<KnownPackage>  _inputKnownCCList   = new List<KnownPackage>();

        #endregion

        #region constructor

        /// <summary>
        /// initializes the form
        /// </summary>
        public ManageKnownPackagesInfoGui()
        {
            InitializeComponent();
        }

        #endregion

        #region methods

        /// <summary>
        /// handles the form load event
        /// reads the known packages information from the xml file
        /// fills the data grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageKnownPackagesInfoGui_Load(object sender, EventArgs e)
        {
            _isFormLoading = true;
            
            //setup binding source
            bindingSourceReadInInfo.DataSource = _inputKnownCCList;            
            
            //setup error provider
            errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;          
            
            //fill datagrid with values from the xml file
            loadKnownPackageInfo();

            btOk.Focus();

            _isFormLoading = false;
        }     

        /// <summary>
        /// handles the add button click event
        /// adds current values from the input textboxes to the datagrid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btAdd_Click(object sender, EventArgs e)
        {
            addCurrentItemToDataGrid();
        }

        /// <summary>
        /// adds all read items to datagrid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btAddAll_Click(object sender, EventArgs e)
        {
            while (bindingSourceReadInInfo.Count > 0)
            {
                bindingSourceReadInInfo.Position = 0;
                updateInputTextboxes();
                
                //cancel when errors occur
                if (!addCurrentItemToDataGrid())
                {
                    break;
                }                
            }
           
        }

        /// <summary>
        /// adds the current displayed information item to the datagrid
        /// validates the input
        /// package id, and names have to be set
        /// notifies about primary key conflicts
        /// </summary>
        /// <returns>true if adding successful</returns>
        private bool addCurrentItemToDataGrid()
        {
            if (!validateInput())
                return false;

            try
            {
                _dataSetPackages.Tables[0].Rows.Add(txtPackageId.Text, txtName_DE.Text, txtName_EN.Text, txtCreator.Text, txtHomepage.Text);


                removeCurrentKnownCCItem();
                updateInputTextboxes();
                return true;

            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(ConstraintException))
                {
                    errorProvider.Clear();
                    errorProvider.SetError(txtPackageId, ex.Message);
                }
                else
                {
                    MessageBox.Show(string.Format(errors.errorAddKnownCCInfo, ex.Message), errors.errorAddKnownCCInfoTitle);
                }
                return false;
            }
        }

        /// <summary>
        /// removes the current KnownCC item fromm the binding source 
        /// </summary>
        private void removeCurrentKnownCCItem()
        {
            if (bindingSourceReadInInfo.Count > 0)
            {
                bindingSourceReadInInfo.RemoveCurrent();
            }
            
            updateInputTextboxes();
        }

        /// <summary>
        /// checks if all necessary textboxes are filled
        /// </summary>
        /// <returns>true when all necessary textboxes are filled</returns>
        private bool validateInput()
        {
            bool isValid = true;

            errorProvider.Clear();

            if (string.IsNullOrEmpty(txtName_DE.Text))
            {
                errorProvider.SetError(txtName_DE, errors.errorKnownCCNameIsEmpty);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtName_EN.Text))
            {
                errorProvider.SetError(txtName_EN, errors.errorKnownCCNameIsEmpty);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtPackageId.Text))
            {
                errorProvider.SetError(txtPackageId, errors.errorKnownCCPackageIdIsEmpty);
                isValid = false;
            }
            return isValid;
        }

        /// <summary>
        /// handles the click event on the commit button
        /// the data set values are written to the xml file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCommit_Click(object sender, EventArgs e)
        {
            commitKnownPackageInfo();
            
        }

        /// <summary>
        /// handles the form closing event
        /// notifies about changed known cc info
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>       
        private void ManageKnownPackagesInfoGui_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_isDataUpdated)
            {
                OnPackagesInfoUpdate();
            }
            if (this.backgroundWorkerReadFiles.IsBusy)
            {
                MessageBox.Show(messages.messageKnownCCCantCloseForm, messages.messageKnownCCCantCloseFormTitle);
                e.Cancel = true;
            }
            else if (this.DialogResult == DialogResult.OK)
            {
                //ask for committing unsaved changes
                if (btCommit.Enabled == true)
                {                   
                    commitKnownPackageInfo();
                    OnPackagesInfoUpdate();
                }
            }

        }

        /// <summary>
        /// handles the cancel background process button click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCancelBackgroundProcess_Click(object sender, EventArgs e)
        {
            this.backgroundWorkerReadFiles.CancelAsync();
        }

        /// <summary>
        /// updates the textbox values
        /// </summary>
        private void updateInputTextboxes()
        {

            if (bindingSourceReadInInfo.Position >= 0)
            {
                KnownPackage knownPackage = bindingSourceReadInInfo.Current as KnownPackage;

                txtPackageId.Text = knownPackage.Guid;
                txtName_DE.Text = knownPackage.ItemNameDE;
                txtName_EN.Text = knownPackage.ItemNameEN;
            }
            else
            {
                txtPackageId.Text = "";
                txtName_DE.Text = "";
                txtName_EN.Text = "";
            }


        }

        /// <summary>
        /// updates the binding source
        /// </summary>
        private void updateBindingSource()
        {
            bindingSourceReadInInfo.ResetBindings(false);
        }

        /// <summary>
        /// handles the binding source list changed event
        /// checks if the binding source has elements
        /// enables / diables the add buttons accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingSourceReadInInfo_DataSourceChanged(object sender, ListChangedEventArgs e)
        {
            if (bindingSourceReadInInfo.Count > 0)
            {
                btAdd.Enabled = true;
                btAddAll.Enabled = true;
            }
            else
            {
                btAdd.Enabled = false;
                btAddAll.Enabled = false;
            }
        }

        #region read/write xml

        /// <summary>
        /// saves the datagrid to xml
        /// </summary>
        private void commitKnownPackageInfo()
        {
            bool isCommitSuccess = false;            

            //rename columns for export
            _dataSetPackages.Tables[0].Columns[0].Caption       = datatableIDs.manageKnownCCColumnPackageId;
            _dataSetPackages.Tables[0].Columns[0].ColumnName    = datatableIDs.manageKnownCCColumnPackageId;
            _dataSetPackages.Tables[0].Columns[1].Caption       = datatableIDs.manageKnownCCColumnNameDE;
            _dataSetPackages.Tables[0].Columns[1].ColumnName    = datatableIDs.manageKnownCCColumnNameDE;
            _dataSetPackages.Tables[0].Columns[2].Caption       = datatableIDs.manageKnownCCColumnNameEN;
            _dataSetPackages.Tables[0].Columns[2].ColumnName    = datatableIDs.manageKnownCCColumnNameEN;
            _dataSetPackages.Tables[0].Columns[3].Caption       = datatableIDs.manageKnownCCColumnCreator;
            _dataSetPackages.Tables[0].Columns[3].ColumnName    = datatableIDs.manageKnownCCColumnCreator;
            _dataSetPackages.Tables[0].Columns[4].Caption       = datatableIDs.manageKnownCCColumnHomepage;
            _dataSetPackages.Tables[0].Columns[4].ColumnName    = datatableIDs.manageKnownCCColumnHomepage;

            this.Cursor = Cursors.WaitCursor;

            try
            {
                _dataSetPackages.WriteXml(FilePaths.knownCCXmlDataPath);
                isCommitSuccess = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(    string.Format(errors.errorKnownOrBadCCCommit, ex.Message), 
                                    errors.errorKnownOrBadCCCommitTitle,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }            


            this.Cursor = Cursors.Default;

            //rename colums for gui
            _dataSetPackages.Tables[0].Columns[0].Caption = names.manageKnownCCGuiColumnPackageId;
            _dataSetPackages.Tables[0].Columns[0].ColumnName = names.manageKnownCCGuiColumnPackageId;
            _dataSetPackages.Tables[0].Columns[1].Caption = names.manageKnownCCGuiColumnNameDE;
            _dataSetPackages.Tables[0].Columns[1].ColumnName = names.manageKnownCCGuiColumnNameDE;
            _dataSetPackages.Tables[0].Columns[2].Caption = names.manageKnownCCGuiColumnNameEN;
            _dataSetPackages.Tables[0].Columns[2].ColumnName = names.manageKnownCCGuiColumnNameEN;
            _dataSetPackages.Tables[0].Columns[3].Caption = names.manageKnownCCGuiColumnCreator;
            _dataSetPackages.Tables[0].Columns[3].ColumnName = names.manageKnownCCGuiColumnCreator;
            _dataSetPackages.Tables[0].Columns[4].Caption = names.manageKnownCCGuiColumnHomepage;
            _dataSetPackages.Tables[0].Columns[4].ColumnName = names.manageKnownCCGuiColumnHomepage;

            dataGridViewKnownCC.Columns[0].Width = 220;
            dataGridViewKnownCC.Columns[1].Width = 180;
            dataGridViewKnownCC.Columns[2].Width = 180;
            dataGridViewKnownCC.Columns[3].Width = 110;
            dataGridViewKnownCC.Columns[4].Width = 180;

            if (isCommitSuccess)
            {
                btCommit.Enabled = false;
                _isDataUpdated = true;
            }
            
        }

        /// <summary>
        /// loads info about known packages from xml to datagrid
        /// </summary>
        private void loadKnownPackageInfo()
        {
            //create dataset and table
            _dataSetPackages = new DataSet();
            _dataSetPackages.DataSetName = datatableIDs.manageKnownCCDatasetName;
            DataTable dataTable = new DataTable(datatableIDs.manageKnownCCDataTableName);
            dataTable.Columns.Add(datatableIDs.manageKnownCCColumnPackageId, typeof(string));
            dataTable.Columns.Add(datatableIDs.manageKnownCCColumnNameDE, typeof(string));
            dataTable.Columns.Add(datatableIDs.manageKnownCCColumnNameEN, typeof(string));
            dataTable.Columns.Add(datatableIDs.manageKnownCCColumnCreator, typeof(string));
            dataTable.Columns.Add(datatableIDs.manageKnownCCColumnHomepage, typeof(string));

            //package id is primary key
            DataColumn[] keys = new DataColumn[1];
            keys[0] = dataTable.Columns[0];
            dataTable.PrimaryKey = keys;

            //add table to dataset
            _dataSetPackages.Tables.Add(dataTable);

            //read xml info to dataset
            this.Cursor = Cursors.WaitCursor;

            try
            {
                _dataSetPackages.ReadXml(FilePaths.knownCCXmlDataPath, XmlReadMode.Auto);
            }
            catch (Exception ex)
            {

                MessageBox.Show(string.Format(errors.errorKnownOrBadCCLoad, ex.Message),
                                    errors.errorKnownOrBadCCLoadTitle,
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            this.Cursor = Cursors.Default;

            //set column names for gui
            _dataSetPackages.Tables[0].Columns[0].Caption = names.manageKnownCCGuiColumnPackageId;
            _dataSetPackages.Tables[0].Columns[0].ColumnName = names.manageKnownCCGuiColumnPackageId;
            _dataSetPackages.Tables[0].Columns[1].Caption = names.manageKnownCCGuiColumnNameDE;
            _dataSetPackages.Tables[0].Columns[1].ColumnName = names.manageKnownCCGuiColumnNameDE;
            _dataSetPackages.Tables[0].Columns[2].Caption = names.manageKnownCCGuiColumnNameEN;
            _dataSetPackages.Tables[0].Columns[2].ColumnName = names.manageKnownCCGuiColumnNameEN;
            _dataSetPackages.Tables[0].Columns[3].Caption = names.manageKnownCCGuiColumnCreator;
            _dataSetPackages.Tables[0].Columns[3].ColumnName = names.manageKnownCCGuiColumnCreator;
            _dataSetPackages.Tables[0].Columns[4].Caption = names.manageKnownCCGuiColumnHomepage;
            _dataSetPackages.Tables[0].Columns[4].ColumnName = names.manageKnownCCGuiColumnHomepage;

            dataGridViewKnownCC.DataSource = _dataSetPackages.Tables[0];

            dataGridViewKnownCC.Columns[0].Width = 220;
            dataGridViewKnownCC.Columns[1].Width = 180;
            dataGridViewKnownCC.Columns[2].Width = 180;
            dataGridViewKnownCC.Columns[3].Width = 110;
            dataGridViewKnownCC.Columns[4].Width = 180;
        }

        #endregion       

        #region read KnownCC info in

        /// <summary>
        /// handles the open package button event
        /// opens the chosen sims3pack file and reads the packageId
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btOpenPackage_Click(object sender, EventArgs e)
        {
            try
            {
                ChooseFileOrFolderGui chooseFileOrFolderDialog = new ChooseFileOrFolderGui();
                DialogResult result = chooseFileOrFolderDialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    string[] fileNames;

                    this.Cursor = Cursors.WaitCursor;
                    
                    if (chooseFileOrFolderDialog.IsChooseFolder == true)
                    {
                        fileNames = openFolder();
                    }
                    else
                    {
                        fileNames = openFiles();
                    }

                    labelStateMessage.Text = "";

                    if (fileNames != null)
                    {
                        if (fileNames.GetLength(0) > 0)
                        {
                            //start reading the info in a background process
                            labelStateMessage.Text = messages.messageKnownCCReadInformation;
                            btCancelBackgroundProcess.Enabled = true;
                            backgroundWorkerReadFiles.RunWorkerAsync(fileNames);
                        }
                        else
                        {
                            labelStateMessage.Text = messages.messageKnownCCReadInfoNoFileFound;
                            progressBar.Value = 0;
                        }
                    }
                    else
                    {
                        labelStateMessage.Text = messages.messageKnownCCReadInfoNoFileFound;
                        progressBar.Value = 0;
                    }

                    this.Cursor = Cursors.Default;
                }
                else
                {
                    labelStateMessage.Text = "";
                }
            }
            catch (Exception ex)
            {
                labelStateMessage.Text = "";
                this.Cursor = Cursors.Default;
               
                MessageBox.Show(    string.Format(errors.errorKnownCCOpenFile, ex.Message),
                                    errors.errorKnownCCOpenFileTitle,MessageBoxButtons.OK ,
                                    MessageBoxIcon.Error);
            }

        }

        /// <summary>
        /// uses open file dialog
        /// retrieves filenames
        /// </summary>
        /// <returns>filenames</returns>
        private string[] openFiles()
        {
            string[] fileNames = null;

            labelStateMessage.Text = messages.messageKnownCCSGetFilesForReadingInfos;

                DialogResult result = openSims3PackDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    fileNames = openSims3PackDialog.FileNames;
                }
                else
                {
                    labelStateMessage.Text = "";                    
                }

            return fileNames;
        }

        /// <summary>
        /// uses choose folder dialog
        /// retrieves filenames
        /// searches given folder and subfolders
        /// </summary>
        /// <returns></returns>
        private string[] openFolder()
        {
            string[] fileNames = null;
            string dirName;

            labelStateMessage.Text = messages.messageKnownCCSGetFilesForReadingInfos;

            DialogResult result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                dirName = folderBrowserDialog.SelectedPath;
                
                List<string> fileNameList = new List<string>();

                //recursive search for sims3pack files
                dirSearch(dirName, fileNameList);

                fileNames = fileNameList.ToArray();
            }
            else
            {
                labelStateMessage.Text = "";
            }

            return fileNames;
        }

        /// <summary>
        /// searches a directory and all its subdirs for sims3pack files
        /// </summary>
        /// <param name="sDir">parent directory</param>
        /// <param name="fileNameList">file name list</param>
        private void dirSearch(string sDir, List<string> fileNameList)
        {          
            
            try
            {
                foreach (string fileName in Directory.GetFiles(sDir, "*.sims3pack", SearchOption.AllDirectories))
                {
                    fileNameList.Add(fileName);
                }
            }
            catch (Exception)
            {
                throw;               
            }
        }

        #endregion

        #region background worker

        /// <summary>
        /// handles the background worker do work event
        /// do time consuming work here
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            string[] fileNames = (string[])e.Argument;

            int numberFiles = fileNames.GetLength(0);
            double percentForOneFile = 100.00 / numberFiles;
            double percentProgress = 0.0;

            Sims3PackReaderClass reader = new Sims3PackReaderClass();
            string packageId, nameDE, nameEN;

            foreach (string fileName in fileNames)
            {
               
                
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    try
                    {
                        reader.readPackageIdDisplayNameFromSims3Pack(fileName, out packageId, out nameDE, out nameEN);

                        ////add every package id once
                        //if (!_inputKnownCCDict.ContainsKey(packageId))
                        //{
                            KnownPackage knownPackage = new KnownPackage();
                            knownPackage.Guid = packageId;
                            knownPackage.ItemNameDE = nameDE;
                            knownPackage.ItemNameEN = nameEN;
                            
                            _inputKnownCCList.Add(knownPackage);
                            //_inputKnownCCDict.Add(packageId, packageId);
                        //}
                        //else
                        //{
                        //    _filesNotReadList.Add(fileName);
                        //}

                        percentProgress += percentForOneFile;
                        worker.ReportProgress((int)percentProgress);
                    }
                    catch(Exception)
                    {
                        throw;
                    }
                }

                
            }
        }

        /// <summary>
        /// handles the backgroundworker progress changed event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            this.progressBar.Value = e.ProgressPercentage;            
        }
        
        /// <summary>
        /// handles the background worker runworkerCompleted event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            btCancelBackgroundProcess.Enabled = false;
            
            if (e.Cancelled == true)
            {
                labelStateMessage.Text = messages.messageKnownCCReadInformationAbort;                
            }
            else if (e.Error != null)
            {
                labelStateMessage.Text = messages.messageKnownCCReadInformationError;                

                MessageBox.Show(string.Format(errors.errorKnownCCOpenFile, e.Error.Message),
                                    errors.errorKnownCCOpenFileTitle, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);

                this.progressBar.Value = 0;
                
            }
            else
            {
                this.progressBar.Value = 100;
                labelStateMessage.Text = messages.messageKnownCCReadInformationComplete;      
            }

            updateBindingSource();
            updateInputTextboxes();
        }

        #endregion     

        #region binding navigator event handler

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            updateInputTextboxes();
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            updateInputTextboxes();
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            updateInputTextboxes();
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            updateInputTextboxes();
        }

        private void btRemoveCurrentItem_Click(object sender, EventArgs e)
        {
            updateInputTextboxes();
        }

        /// <summary>
        /// handles binding navigator key down event
        /// sets the binding source to the entered position
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingNavigatorPositionItem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string positionText = bindingNavigatorKnownCC.PositionItem.Text;

                try
                {
                    int position = Convert.ToInt32(positionText);
                    bindingSourceReadInInfo.Position = position - 1;
                    updateInputTextboxes();
                }
                catch
                {

                }
            }
        }

        #endregion

        #region textbox textchanged event handler

        private void txtPackageId_TextChanged(object sender, EventArgs e)
        {
            errorProvider.Clear();
            setAddButtonState(sender as TextBox);
        }

        private void txtName_DE_TextChanged(object sender, EventArgs e)
        {
            errorProvider.Clear();
            setAddButtonState(sender as TextBox);
        }

        private void txtName_EN_TextChanged(object sender, EventArgs e)
        {
            errorProvider.Clear();
            setAddButtonState(sender as TextBox);
        }

        /// <summary>
        /// checks if the add button has to get enabled or disabled
        /// </summary>
        private void setAddButtonState(TextBox txtBox)
        {
            if (!string.IsNullOrEmpty(txtBox.Text))
            {
                btAdd.Enabled = true;                
            }
            else if (string.IsNullOrEmpty(txtName_EN.Text) && string.IsNullOrEmpty(txtName_DE.Text) && string.IsNullOrEmpty(txtPackageId.Text))
            {
                btAdd.Enabled = false;
            }
        }
        #endregion

        #region datagridview updated event handler

        /// <summary>
        /// handles the cell value changed event of the datagrid
        /// enables the commit button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewKnownCC_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!_isFormLoading)
            {
                btCommit.Enabled = true;
            }
        }

        /// <summary>
        /// handles the rows added event of the datagrid
        /// enables the commit button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewKnownCC_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (!_isFormLoading)
            {
                btCommit.Enabled = true;
            }
        }

        /// <summary>
        /// handles the rows removed event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewKnownCC_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (!_isFormLoading)
            {
                btCommit.Enabled = true;
            }
        }

        #endregion

       

        #endregion

    }
}
